# Name:    CProgressWndRnd.py
# Date:    Sat May  1 20:56:56 CEST 2010
# Purpose: Show random status of progress bar. It is used while running long-time operation.
# Depends: CThread
# Def:     CProgressWndRnd()
# Inputs:  

from threading import Event
import random, time
import gtk
import gobject

# my libraries
from CThread import CThread


####################################################################################################

class CProgressWndRnd(CThread):
	'''
	''Show random status of progress bar
	'''
	__NAME='CProgressWndRnd Thread'

	__width = 450
	__height = 40
	__delay = 0.3

	event = None

#---------------------------------------------------------------------------------------------------

	def __init__(self, text, parentWnd = None):
		super(CProgressWndRnd, self).__init__(self.__NAME)

		self.threadEvent = Event()
		
		#self.wnd = gtk.Dialog(text, parentWnd, gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR)
		self.wnd = gtk.Window(gtk.WINDOW_POPUP)
		self.wnd.set_position(gtk.WIN_POS_CENTER)
		self.wnd.set_size_request(self.__width, self.__height)

		self.progBar = gtk.ProgressBar()
		self.progBar.set_text(text)

		self.wnd.add(self.progBar)

		self.wnd.connect('destroy', self.stop)

		#gtk.gdk.threads_init()
		gobject.threads_init()

#---------------------------------------------------------------------------------------------------
	
	def run(self):
		#While the stopthread event isn't setted, the thread keeps going on

		#self.wnd.show_all()
		gobject.idle_add(self.wnd.show_all)

		while not self.threadEvent.isSet():
			# Acquiring the gtk global mutex
			#gtk.gdk.threads_enter()

			#Setting a random value for the fraction
			#self.progBar.set_fraction(random.random())
			gobject.idle_add(self.progBar.set_fraction, random.random())
			# Releasing the gtk global mutex
			#gtk.gdk.threads_leave()
			
			time.sleep(self.__delay)

		#self.wnd.hide_all()
		gobject.idle_add(self.wnd.hide_all)

		super(CProgressWndRnd, self).run()
			
#---------------------------------------------------------------------------------------------------
	
	def stop(self, waitFor = None):
		if waitFor is not None:
			waitFor.join()
		self.threadEvent.set()

#---------------------------------------------------------------------------------------------------

	def getHeight(self):
		return self.__height
	
#---------------------------------------------------------------------------------------------------

	def setHeight(self, height):
		self.__height = height
	
#---------------------------------------------------------------------------------------------------

	def getWidth(self, width):
		return self.__width
	
#---------------------------------------------------------------------------------------------------

	def setWidth(self, width):
		self.__width = width
	
#---------------------------------------------------------------------------------------------------

	def hideAll(self):
		if not self.threadEvent.isSet():
			self.wnd.hide_all()
	
#---------------------------------------------------------------------------------------------------

	def showAll(self):
		if not self.threadEvent.isSet():
			self.wnd.show_all()
	
####################################################################################################
